package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopGoods;
import com.bdqn.pojo.ShopGoodsCountVO;
import com.bdqn.service.ShopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="商品接口")
@RestController
@RequestMapping("/shopgoods")
public class ShopGoodsController {

    @Autowired
    private ShopGoodsService shopGoodsService;

    /**
     * 新增商品记录
     *
     * @param shopGoods
     * @return R
     */
    @ApiOperation("新增商品记录")
    @WebLogger("新增商品记录")
    @PostMapping("/add")
    public R save(@ApiParam("商品对象") @RequestBody ShopGoods shopGoods) {
        return new R<>(shopGoodsService.save(shopGoods));
    }

    /**
     * 修改商品记录
     *
     * @param shopGoods
     * @return R
     */
    @ApiOperation("修改商品记录")
    @WebLogger("修改商品记录")
    @PostMapping("/update")
    public R update(@ApiParam("商品对象") @RequestBody ShopGoods shopGoods) {
        return new R<>(shopGoodsService.updateById(shopGoods));
    }

    /**
     * 通过id删除一条商品记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条商品记录")
    @WebLogger("通过id删除一条商品记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("商品id") Integer id) {
        return new R<>(shopGoodsService.removeById(id));
    }

    /**
     * 通过id查询单条商品记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条商品记录")
    @WebLogger("通过id查询单条商品记录")
    @GetMapping("/{id}")
    public R<ShopGoods> getById(@ApiParam("商品id") @PathVariable Integer id) {
        return new R<>(shopGoodsService.getById(id));
    }

    /**
     * 通过商品信息查询商品记录
     *
     * @param shopGoods 商品信息对象
     * @return R
     */
    @ApiOperation("通过商品信息查询商品记录")
    @WebLogger("通过商品信息查询商品记录")
    @GetMapping("/list")
    public R<List<ShopGoods>> getList(@ApiParam("查询的商品对象") ShopGoods shopGoods) {
        return new R<>(shopGoodsService.getList(shopGoods));
    }

    /**
     * 分页查询商品
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopGoods 商品
     * @return
     */
    @ApiOperation("分页查询商品")
    @WebLogger("分页查询商品")
    @GetMapping("/page")
    public R<IPage<ShopGoods>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的商品对象") ShopGoods shopGoods) {
        return new R<>(shopGoodsService.getPage(new Page<>(pageNo, pageSize), shopGoods));
    }

    /**
     * 分页查询商品数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopGoods 商品
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询商品数据")
    @WebLogger("分页查询商品数据")
    @GetMapping("/pagelist")
    public R<List<ShopGoods>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的商品对象") ShopGoods shopGoods) {
        IPage<ShopGoods> page = shopGoodsService.getPage(new Page<>(pageNo, pageSize), shopGoods);
        return new R<>(page.getRecords(), page.getTotal());
    }


    /**
     * 用户数统计
     *
     * @return
     */
    @ApiOperation("商品数统计")
    @WebLogger("商品数统计")
    @GetMapping("/countRate")
    public R<ShopGoodsCountVO> getCountRate() {

        return new R<>(shopGoodsService.getCountRate());
    }

}
